using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Acms.DataControls
{
    [System.ComponentModel.Designer(typeof(SiteMappingDataSourceDesigner))]
    [System.Web.UI.ToolboxData("<{0}:SiteMappingDataSource runat=server></{0}:SiteMappingDataSource>")]
    public class SiteMappingDataSource : GenericDataSource<Bll.SiteMapping>
    {
        public enum Fields
        {
            Id,
            SiteId,
            MappingName,
            ConcurencyId
        }

        protected override Acms.Bll.SiteMapping PopulateData(UpdateAction action, System.Collections.IDictionary keys, System.Collections.IDictionary values, System.Collections.IDictionary oldValues)
        {
            Bll.SiteMapping tmp = null;
            byte[] concurencyId = null;
            if (action == UpdateAction.Insert)
            {
                tmp = Bll.SiteMapping.CreateInstance();
            }
            else
            {
                int id = GetValue<int>(Fields.Id, keys);
                concurencyId = GetValue<byte[]>(Fields.ConcurencyId, keys);
                tmp = Bll.SiteMapping.GetById(id);
            }

            if (tmp != null)
            {
                tmp.SiteId = SetValue(tmp.SiteId, Fields.SiteId, values, oldValues);
                tmp.MappingName = SetValue(tmp.MappingName, Fields.MappingName, values, oldValues);
            }
            return tmp;
        }

        protected override int SaveChanges(Acms.Bll.SiteMapping item)
        {
            item.Update();
            return 1;
        }

        protected override int Delete(Acms.Bll.SiteMapping item)
        {
            item.Delete();
            return 1;
        }

        protected override void AddInsertedKey(Acms.Bll.SiteMapping item, System.Collections.IDictionary values)
        {
            values[Fields.Id] = item.Id;
        }
    }

    public class SiteMappingDataSourceDesigner : GenericDataSourceDesigner<Bll.SiteMapping>
    {
        protected override Acms.Bll.SiteMapping GetDesignTimeItem(int index)
        {
            string[] names = new string[]{"www.acmssite.com", "localhost/development", "192.168.1.1/mysite", "everyone.allmysites.com"};
            Random rnd = new Random();
            Acms.Bll.SiteMapping tmp = Acms.Bll.SiteMapping.CreateInstance();
            tmp.SiteId = rnd.Next();
            tmp.MappingName = names[rnd.Next(0, names.Length)];
            return tmp;
        }
    }
}
